package com.nowcoder.community.community.controller;

import com.nowcoder.community.community.entity.DiscussPost;
import com.nowcoder.community.community.entity.Page;
import com.nowcoder.community.community.entity.User;
import com.nowcoder.community.community.service.DiscussPostService;
import com.nowcoder.community.community.service.MessageService;
import com.nowcoder.community.community.service.UserService;
import com.nowcoder.community.community.util.HostHolder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class HomeController {
    private static final Logger logger = LoggerFactory.getLogger(UserController.class);

    @Autowired
    private DiscussPostService discussPostService;

    @Autowired
    private UserService userService;

    @Autowired
    private HostHolder hostHolder;

    @Autowired
    private MessageService messageService;


    @RequestMapping(path = "/index", method = RequestMethod.GET)
    public String getIndexPage(ModelMap model, Page page){
        User user = hostHolder.getUser();
        //方法调用前会自动实例化Model并且将page注入model，在thymeleaf可以直接访问Page中的数据
        page.setRows(discussPostService.findDiscussPostRows(0));
        page.setPath("/index");
//        System.out.println(page.getLimit());
//        System.out.println(page.getOffset());
//        System.out.println(page.getTotal());
        //获取评论列表
        List<DiscussPost> discussPostss = discussPostService.findDiscussPosts(0, page.getOffset(), page.getLimit());
        List<Map<String, Object>> discussPosts = new ArrayList<>();
        if(discussPostss != null){
            for (DiscussPost post : discussPostss) {//将user和评论封装在一起
                Map<String, Object> map = new HashMap<>();
                map.put("post",post);
                User userById = userService.findUserById(post.getUserId());
                map.put("user",userById);
                discussPosts.add(map);
            }
        }
        model.addAttribute("discussPosts", discussPosts);


        return "/index";
    }

    @RequestMapping(path = "/error", method = RequestMethod.GET)
    public String getErrorPage() {
        return "/error/500";
    }


    /**
     * 点赞功能，采用点击路径获取对应的帖子ID，然后对其进行操作
     */
    @RequestMapping(path = "/addponit/{discussPostId}", method = RequestMethod.GET)
    public String addPonitCount(@PathVariable("discussPostId") int discussPostId) {

        DiscussPost post = discussPostService.getDiscussPost(discussPostId);
        discussPostService.updateCommentPonit(post.getId(), post.getCommentPonit() + 1);
//        logger.debug("addpoti:"+String.valueOf(post.getId() + post.getCommentPonit())+"\n");
        return "redirect:/index";
    }

}
